/**
 * 作者: hxl
 * 日期: 2025-03-10 11:28:53
 * 备注: 跨服冲榜分组数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class RankActGroupModel extends BaseModel implements Model {
    public static final String LISTKEY = "rank_id.round";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_NOT;// 数据入库类型 不入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 唯一
    protected int rankId; // 活动分组ID
    protected long round; // 期数
    protected String serverList = ""; // 服务器id

    public void reset() {
    }

    protected String getKey() {
        return "RankActGroup." + rankId + "." + round;
    }

    protected String getListKey() {
        return "RankActGroup." + rankId;
    }

    protected String getListItemKey() {
        return round + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("rank_act_group"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 唯一
        obj.put("rankId", rankId); // 活动分组ID
        obj.put("round", round); // 期数
        obj.put("serverList", serverList); // 服务器id
        return obj;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getRankId() {
        return rankId;
    }

    public void putRankId(int rankId) {
        if (this.rankId != rankId) {
            this.rankId = rankId;
            updateRedisHashField(getKey(), "rankId", this.rankId);
        }
    }

    public void addRankId(int value) {
        this.rankId += value;
        updateRedisHashField(getKey(), "rankId", this.rankId);
    }

    public long getRound() {
        return round;
    }

    public void putRound(long round) {
        if (this.round != round) {
            this.round = round;
            updateRedisHashField(getKey(), "round", this.round);
        }
    }

    public void addRound(long value) {
        this.round += value;
        updateRedisHashField(getKey(), "round", this.round);
    }

    public JsonArray getServerListArray() {
        if (this.serverList != null && this.serverList.length() > 0) {
            return new JsonArray(this.serverList);
        } else {
            return new JsonArray();
        }
    }

    public String getServerList() {
        return serverList;
    }

    public void putServerList(String serverList) {
        if (!this.serverList.equals(serverList)) {
            this.serverList = serverList;
            updateRedisHashField(getKey(), "serverList", this.serverList);
        }
    }

}